package cn.me.demo1;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * @author jia
 * @date 2021/2/19
 * 多线程同步下载
 */
public class TestThread2 extends Thread {
    private String url;//网络图片地址
    private String name;//保存的文件名

    public TestThread2(String url, String name) {
        this.name = name;
        this.url = url;
    }

    @Override
    public void run() {
        WebDownloader webDownloader = new WebDownloader();
        webDownloader.downloader(url, name);
        System.out.println("下载文件名为：" + name);
    }

    public static void main(String[] args) {

        TestThread2 t1 = new TestThread2("https://www.kuangstudy.com/assert/course/c1/01.jpg", "1.jpg");
        TestThread2 t2 = new TestThread2("https://www.kuangstudy.com/assert/course/c1/01.jpg", "2.jpg");
        TestThread2 t3 = new TestThread2("https://www.kuangstudy.com/assert/course/c1/01.jpg", "3.jpg");
    /*
     * 顺序不一定按照123的顺序
     */
        t1.start();
        t2.start();
        t3.start();
    }
}

class WebDownloader {
    public void downloader(String url, String name) {
        try {
            FileUtils.copyURLToFile(new URL(url), new File(name));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("IO异常");
        }
    }
}
